package com.wx.plugin.context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * <p></p>
 *
 * @author  yangz
 * @date    2016/11/20
 * @version latest
 */
public class ThreadContext {
    public final static String KEY_OPENID = "KEY_OPENID";
    public final static String KEY_FOLLOWER = "KEY_FOLLOWER";
    public final static String KEY_YZM = "KEY_YZM";
    public final static String KEY_SIGN = "KEY_SIGN";
    public final static String COOKIE_KEY_YAOQINGMA = "COOKIE_KEY_YAOQINGMA";
    private static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>();

    public static void set(String key, Object value){
        Map<String, Object> stringObjectMap = threadLocal.get();
        if(stringObjectMap == null){
            Map<String, Object> map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        Map<String, Object> objectMap = threadLocal.get();
        objectMap.put(key, value);
    }

    public static Object get(String key){
        Map<String, Object> stringObjectMap = threadLocal.get();
        if(stringObjectMap == null){
            return null;
        }else{
            return stringObjectMap.get(key);
        }
    }

    public static void setHttp(HttpServletRequest req, HttpServletResponse resp){
        set("request", req);
        set("response", req);
    }
    public static void removeHttp(){
        set("request", null);
        set("response", null);
    }
    public static HttpServletRequest getRequest(){
        Object request = get("request");
        return (HttpServletRequest) request;
    }
    public static HttpServletResponse getResponse(){
        Object response = get("response");
        return (HttpServletResponse) response;
    }
}
